<?php
namespace Cyfy\Modules\Menu\Forms;

use \Cyfy\Modules\Forms;
/**
 * Creates a new menu
 */
class Create extends Forms\Form
{
	function structure()
    {
        $this -> permission("Menu", "create and edit");
		
		$this -> action = "administration/Menu/menus";
	
		$i = new Forms\TextBox("name", self :: t(10));
		$i -> required = true;
		$i -> maxlength = 50;
		$this -> addItem($i);
		
		$i = new Forms\TextBox("code", self :: t(20));
		$i -> required = true;
		$i -> maxlength = 50;
		$this -> addItem($i);
		
		$b = new Forms\Button(self :: t(30));
		$this -> addButton($b);
    }
	
    function validate()
    {
        // used code?
        if(\dibi :: query("SELECT count(*) FROM [:cyfy:Menu-menus] WHERE [code] = %s", $this -> get('code')) -> fetchSingle() != 0){
            \Cyfy\Message :: set(self :: t(41, array($this->get("code"))), \Cyfy\Message::ERROR);
            return false;
        }
        return true;
    }
  
    function submit()
    {
        $values = array(
            'name' => $this -> get('name'),
            'code' => $this -> get('code')
        );
        \dibi :: query("INSERT INTO [:cyfy:Menu-menus] %v", $values);
        \Cyfy\Message :: set(self :: t(51, array($this->get("name"))));
    }
}
